/* OSM layers for gvSIG. 
 * Geographic Information System of the Valencian Government
*
* Copyright (C) 2012 Nacho Brodin
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
* MA  02110-1301, USA.
* 
*/
package org.gvsig.raster.osm.downloader;

import org.gvsig.fmap.dal.coverage.RasterLibrary;
import org.gvsig.raster.cache.tile.provider.CacheStruct;
import org.gvsig.raster.cache.tile.provider.Downloader;
import org.gvsig.raster.cache.tile.provider.TileServer;
import org.gvsig.raster.osm.cachestruct.OSMCacheStruct;
import org.gvsig.raster.osm.io.OSMDataParameters;
import org.gvsig.raster.osm.io.OSMProvider;

/** 
* Data server for the tile cache in a OSMProvider 
* @author Nacho Brodin (nachobrodin@gmail.com)
*/
public class OSMTileServer implements TileServer {
	private CacheStruct                struct               = null;
	private Downloader                 downloader           = null;
	private OSMProvider                provider             = null;
	private String                     suffix               = null;
	private int                        tileSize             = 256;
	private int                        levels               = 0;
	
	/**
	 * Creates a OSMTileServer
	 * @param prov
	 * @param suffix
	 * 			it depends on the layer
	 * @param levels
	 * 			it depends on the layer
	 */
	public OSMTileServer(OSMProvider prov, String suffix, int levels) {
		this.provider = prov;
		this.suffix = suffix;
		this.levels = levels;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.gvsig.raster.cache.tile.provider.TileServer#getDownloader()
	 */
	public Downloader getDownloader() {
		if(downloader == null) {
			downloader = new TileDownloaderForOSM(provider, getStruct().getTileSizeByLevel(0)[0]);
		}
		return downloader;
	}

	/*
	 * (non-Javadoc)
	 * @see org.gvsig.raster.cache.tile.provider.TileServer#getStruct()
	 */
	public CacheStruct getStruct() {
		if(struct == null) {
			struct = new OSMCacheStruct(levels, 
					tileSize, 
					tileSize, 
					((OSMDataParameters)provider.getDataParameters()).getOSMLayerName(), 
					RasterLibrary.pathTileCache, 
					suffix, 
					0);
		}
		return struct;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.gvsig.raster.cache.tile.provider.TileServer#setStruct(org.gvsig.raster.cache.tile.provider.CacheStruct)
	 */
	public void setStruct(CacheStruct struct) {
		//La structura de cache es proporcionada por el servidor
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.gvsig.raster.cache.tile.provider.TileServer#getFileSuffix()
	 */
	public String getFileSuffix() {
		return suffix;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.gvsig.raster.cache.tile.provider.TileServer#setFileExtension(java.lang.String)
	 */
	public void setFileSuffix(String extension) {
		this.suffix = extension;
	}
}
